package com.itydl.property.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import com.itydl.property.R;

import static android.animation.ObjectAnimator.ofFloat;

/**
 * 作者：${Howard} on 2018/2/4 10:05
 *
 * @version 1.0
 *          说明：58加载的View
 */
public class LoadingView extends LinearLayout {

    private static String TAG = "LoadingView";

    private final int mTranslationDis;
    private View mShadowView;//阴影
    private ShapeView mShapeView;//图形View
    // 动画执行的时间
    private final long ANIMATOR_DURATION = 500;

    private boolean isViewGone = false;

    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTranslationDis = dip2px(80);
        initLayout();
    }

    /**
     * 初始化组合控件布局
     */
    private void initLayout() {
        // 第三个参数为this,表示布局解析完毕直接添加到LoadingView中（它是一个扩展的LinearLayout）
        inflate(getContext(), R.layout.layout_loading_view, this);
        mShadowView = findViewById(R.id.shadowView);
        mShapeView = (ShapeView) findViewById(R.id.shapeView);

        /**---------  直接开启动画  ---------**/
        post(new Runnable() {
            @Override
            public void run() {
                //让开启动画逻辑在onResume()之后
                startPullDownAnimation();
            }
        });

    }

    /**
     * 开启下落动画
     */
    private void startPullDownAnimation() {
        if(isViewGone){
            return;
        }
        Log.e(TAG,"startPullDownAnimation");
        ObjectAnimator shapeViewDownAnimator = ofFloat(mShapeView,"TranslationY",0,mTranslationDis);
        ObjectAnimator shadowViewDownAnimator = ofFloat(mShadowView,"ScaleX",1.0f,0.3f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(ANIMATOR_DURATION);
        // 下落的速度应该是越来越快,使用加速度插值器
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.playTogether(shapeViewDownAnimator,shadowViewDownAnimator);
        animatorSet.start();

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mShapeView.changeShape();
                //开启回弹动画
                startSpringBackAnimation();
            }
        });
    }

    /**
     * 开启弹起动画
     */
    private void startSpringBackAnimation() {
        if(isViewGone){
            return;
        }
        ObjectAnimator shapeViewDownAnimator = ofFloat(mShapeView,"TranslationY",mTranslationDis,0);
        ObjectAnimator shadowViewDownAnimator = ofFloat(mShadowView,"ScaleX",0.3f,1.0f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(ANIMATOR_DURATION);
        // 下落的速度应该是越来越快,使用加速度插值器
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.playTogether(shapeViewDownAnimator,shadowViewDownAnimator);

        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //开启回弹动画
                startPullDownAnimation();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                //动画开始，开启旋转动画
                startRotateAnimation();
            }
        });

        //开启动画要放在后面，否则onAnimationStart监听不到
        animatorSet.start();

    }

    /**
     * 旋转动画。
     */
    private void startRotateAnimation() {
        ObjectAnimator rotationAnimator = null;
        switch (mShapeView.getCurrentShape()) {
            case Circular:
            case Square:
                //圆形和方形旋转-180度
                rotationAnimator = ofFloat(mShapeView,"rotation",0,180);
                break;
            case Triangle:
                //三角形旋转-120°
                rotationAnimator = ObjectAnimator.ofFloat(mShapeView,"rotation",0,-120);
                break;
            }
        rotationAnimator.setDuration(ANIMATOR_DURATION);
        rotationAnimator.setInterpolator(new DecelerateInterpolator());
        rotationAnimator.start();
    }

    /**
     * 清空动画,清空View
     * @param visibility
     */
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(View.INVISIBLE);
        mShapeView.clearAnimation();
        mShadowView.clearAnimation();

        ViewGroup parent = (ViewGroup) getParent();
        if(parent != null){
            //因为自己装到了父View中了
            parent.removeView(this);
            //移除自己的Views
            removeAllViews();
        }
        isViewGone = true;
    }

    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }
}
